﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WjAutoConfigApp
{
    
    public static class WaitWindowRotate
    {
        private static bool m_bWaitShowing = false;
        private static frm_Wait_Rotate m_frmWait = null;
        private static Thread upgradeThread = null;

        public delegate void CloseEventHandle();
        public static CloseEventHandle CloseEvent;
        public static int ReferenceCount = 0;
        public static bool IsShow
        {
            get { return m_frmWait.Visible; }
        }
        private static void Closed()
        {
            if (CloseEvent != null)
                CloseEvent.Invoke();
            m_bWaitShowing = false;
        }

        public static void Show()
        {
            ReferenceCount++;
            if ((upgradeThread != null) && (upgradeThread.IsAlive == false))
            {
                try
                {
                    upgradeThread.Abort();
                    upgradeThread = null;
                }
                catch (Exception ex)
                {
                }

            }
            if ((m_bWaitShowing == true) || ((upgradeThread != null) && (upgradeThread.IsAlive)))
            {
                return;
            }
            m_bWaitShowing = true;
            m_frmWait = null;
            try
            {
                m_frmWait = new frm_Wait_Rotate();
                m_frmWait.CloseEvent += new frm_Wait_Rotate.CloseEventHandle(Closed);
                upgradeThread = new Thread(ShowWait);
                upgradeThread.IsBackground = true;
                upgradeThread.Start();
            }
            catch (Exception ex)
            {
                m_bWaitShowing = false;
                upgradeThread = null;
                if (m_frmWait != null)
                    m_frmWait.CloseWindow();
                m_frmWait = null;
            }
        }


        private static void ShowWait()
        {
            try
            {
                m_frmWait.TopMost = true;
                m_frmWait.ShowDialog();
            }
            catch (Exception ex)
            {
            }
            m_bWaitShowing = false;
        }

        public static void Hide()
        {
            ReferenceCount--;
            if (m_bWaitShowing == false)
            {
                if (m_frmWait != null)
                {
                    try
                    {
                        m_frmWait.CloseWindow();
                        m_frmWait = null;
                        if (upgradeThread != null)
                            upgradeThread.Abort();
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return;
            }
            m_frmWait.Hide();
            try
            {
                if (m_frmWait != null)
                {
                    if (m_frmWait.InvokeRequired)
                    {
                        object[] pm = new object[1];
                        pm[0] = 1;
                        m_frmWait.BeginInvoke(new Action<int>(DoCloseJob), pm);
                    }
                    else
                    {
                        DoCloseJob(0);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            try
            {
                if ((upgradeThread != null) && (upgradeThread.IsAlive == true))
                {
                    //upgradeThread.Abort();
                    //upgradeThread.Join();
                }
            }
            catch (Exception ex)
            {
            }
            upgradeThread = null;
            m_bWaitShowing = false;
        }

        private static void DoCloseJob(int a)
        {
            try
            {
                if (m_frmWait != null)
                {
                    if (m_frmWait.IsDisposed == false)
                    {
                        m_frmWait.CloseWindow();
                        m_frmWait.Dispose();
                    }

                }
            }
            catch (Exception ex)
            {
            }
            m_frmWait = null;
        }
    }
}
